<?php
namespace Tongcheng\Controller;
use Think\Controller;
class ZhiboController extends CommonController {
    public function index(){
        if(IS_AJAX){
            $model = M("Zhibo");

            $map['wid'] = session('wid');
            $res['count'] = $model->where($map)->count();
            $page = $_GET['page']?$_GET['page']:1;
            $limit = $_GET['limit']?$_GET['limit']:10;
            $res['list'] = $model->where($map)->order("last_time desc")->page("{$page},{$limit}")->select();

            $this->ajaxReturn($res);
        }else{
            $this->display();
        }
    }

    public function add(){
        if(IS_AJAX){
            $model = M("Zhibo");
            $new['wid'] = session('wid');
            $new['title'] = I('post.title');
            $new['video_path'] = I('post.video_path');
            $new['member_id'] = I('post.member_id');
            $new['shop_id'] = I('post.shop_id');
            $new['goods'] = I('post.goods');
            $new['start_time'] = I('post.start_time');
            $new['img'] = I('post.img');
            $new['create_time'] = date("Y-m-d H:i:s");

            if(empty($new['title'])){
                $this->error('请输入标题！');
            }
            if(empty($new['img'])){
                $this->error('请设置封面图片！');
            }
            if(empty($new['video_path'])){
                $this->error('请输入直播流地址！');
            }
            if(!is_numeric($new['member_id'])){
                $this->error('请输入关联用户id！');
            }
            if(!is_numeric($new['shop_id'])){
                $this->error('请输入关联商家id！');
            }
            if(empty($new['goods'])){
                $this->error('请输入关联商品id！');
            }
            $goods = explode(',', $new['goods']);
            foreach ($goods as $k => $v) {
                if(!is_numeric($v)){
                     $this->error('关联商品id格式不正确！');
                }
            }
            if($model->add($new)){
                $this->success('添加成功！');
            }else{
                $this->error('网络忙，请稍后再试！');
            }
        }else{
            $this->display();
        }
    }

    public function edit(){
        $model = M("Zhibo");
        $map['id'] = $_GET['id'];

        if(IS_AJAX){
            $new['wid'] = session('wid');
            $new['title'] = I('post.title');
            $new['video_path'] = I('post.video_path');
            $new['member_id'] = I('post.member_id');
            $new['shop_id'] = I('post.shop_id');
            $new['goods'] = I('post.goods');
            $new['start_time'] = I('post.start_time');
            $new['img'] = I('post.img');
            $new['create_time'] = date("Y-m-d H:i:s");

            if(empty($new['title'])){
                $this->error('请输入标题！');
            }
            if(empty($new['img'])){
                $this->error('请设置封面图片！');
            }
            if(!is_numeric($new['member_id'])){
                $this->error('请输入关联用户id！');
            }
            if(!is_numeric($new['shop_id'])){
                $this->error('请输入关联商家id！');
            }
            if(empty($new['goods'])){
                $this->error('请输入关联商品id！');
            }
            $goods = explode(',', $new['goods']);
            foreach ($goods as $k => $v) {
                if(!is_numeric($v)){
                     $this->error('关联商品id格式不正确！');
                }
            }
            $model->where($map)->save($new);
            $this->success('修改成功！');
        }else{
            $vo = $model->where($map)->find();
            $this->assign('vo',$vo);
            $this->display('add');
        }
    }

    public function top(){
        $model = M("Zhibo");
        $map['wid'] = session('wid');
        $map['id'] = $_POST['id'];

        $new['is_top'] = $_POST['status'];
        if($model->where($map)->save($new)){
            $this->success("置顶成功！");
        }else{
            $this->error("网络忙，请稍后再试！".$model->getLastSql());
        }
    }

    public function tui(){
        $model = M("Zhibo");
        $map['wid'] = session('wid');
        $map['id'] = $_POST['id'];

        $new['is_tui'] = $_POST['status'];
        if($model->where($map)->save($new)){
            $this->success("推荐成功！");
        }else{
            $this->error("网络忙，请稍后再试！");
        }
    }

    public function del(){
        $model = M("Zhibo");
        $map['wid'] = session('wid');
        $map['id'] = $_POST['id'];

        if($model->where($map)->delete()){
            $this->success("删除成功！");
        }else{
            $this->error("网络忙，请稍后再试！");
        }
    }

    public function pass(){
        $model = M("Zhibo");
        $map['wid'] = session('wid');
        $map['id'] = $_POST['id'];

        $info = $model->where($map)->find();

        $eq['start_date'] = $info['start_date'];
        $eq['status'] = 1;

        $list = $model->where($eq)->select();
        $no_arr = array();
        foreach ($list as $k => $v) {
            if($v['start_time'] > $info['start_time'] || $v['end_time'] < $info['end_time']){
                $no_arr[] = $v['rtmp_id'];
            }
        }
        $where['rtmp_id'] = array('not in', $no_arr);
        $rtmp_id = M("ZhiboSite")->where($where)->getField('id');
        if(!$rtmp_id){
            $this->error("该时间段无可用推流地址！");
        }
        $new['rtmp_id'] = $rtmp_id;
        $new['status'] = 1;
        if($model->where($map)->save($new)){
            $this->success("设置成功！");
        }else{
            $this->error("网络忙，请稍后再试！");
        }
    }

    public function nopass(){
        $model = M("Zhibo");
        $map['wid'] = session('wid');
        $map['id'] = $_POST['id'];

        $new['status'] = 2;
        if($model->where($map)->save($new)){
            $this->success("设置成功！");
        }else{
            $this->error("网络忙，请稍后再试！");
        }
    }
}